package com.wushijia.highconcurrency.acomic;

import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * description:AtomicIntegerArray本质上是对int[]类型的封装，使用Unsafe类通过CAS的方式控制int[]在多线程下的安全性，还有AtomicLongArray,AtomicReferenceArray
 *
 * @author yang
 * @date 2018/6/28 20:11
 */
public class AtomicIntegerArrayDemo {

  static AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10);

  public static class AddTask implements Runnable {

    @Override
    public void run() {
      for (int i = 0; i < 10000; i++) {
        atomicIntegerArray.getAndIncrement(i % atomicIntegerArray.length());
      }
    }
  }

  public static void main(String[] args) throws InterruptedException {
    Thread[] threads = new Thread[10];
    for (int i = 0; i < 10; i++) {
      threads[i] = new Thread(new AddTask());
      threads[i].start();
      threads[i].join();
    }
    System.out.println(atomicIntegerArray);
  }

}
